


#include "subject.h"
#include "mutator.h"



// Mutator ::



Mutator::Mutator( CollectionSubjects *subs, CollectionCategories *cats )
    : m_pSubjects( subs )
    , m_pCategories( cats )
{
}



Mutator::~Mutator()
{
    // go through all the subs and restore their state
    restore();
}



/// \return - true if the state is initial or is "like" initial
bool Mutator::isLikeInitial() const
{
    CollectionSubjects::const_iterator is = m_pSubjects->begin(), is_end = m_pSubjects->end();
    for( ; is != is_end; ++is )
    {
        if( !(*is)->isLikeInitial() )
            return false;
    }
    return true;
}



/// mutates the subjects, \return - true if initial state achieved
bool Mutator::mutate()
{    
    // go through all the subs and mutate their state
    CollectionSubjects::iterator is = m_pSubjects->begin(), is_end = m_pSubjects->end();
    for( ; is != is_end; ++is )
    {
        if( !(*is)->mutate() )
            return false;
    }
    return true;
}



/// restores the state of the subjects to initial
void Mutator::restore()
{
    CollectionSubjects::iterator is = m_pSubjects->begin(), is_end = m_pSubjects->end();
    for( ; is != is_end; ++is )
        (*is)->restore();    
}



/// \return true on successfull check
bool Mutator::check()
{
    // tell the cats there was a mutation
    if( m_pCategories )
    {
        CollectionCategories::iterator ic = m_pCategories->begin(), ic_end = m_pCategories->end();
        for( ; ic != ic_end; ++ic )
            (*ic)->hasMutated();
    }

    CollectionSubjects::iterator is = m_pSubjects->begin(), is_end = m_pSubjects->end();
    for( ; is != is_end; ++is )
    {
        CollectionExpressions::iterator ie = (*is)->getExpressions().begin(), ie_end = (*is)->getExpressions().end();
        for( ; ie != ie_end; ++ie )
        {
            if( !(*ie)->check(*is) )
                return false;
        }
    }
    return true;
}
